/// 对话列表项组件
/// 显示单个对话的信息
library;
import 'package:flutter/material.dart';
import 'package:chat_project/config/theme_config.dart';
import 'package:chat_project/models/conversation_model.dart';

class ConversationListItem extends StatelessWidget {
  final Conversation conversation;
  final bool isSelected;
  final VoidCallback onTap;

  const ConversationListItem({
    super.key,
    required this.conversation,
    required this.isSelected,
    required this.onTap,
  });

  @override
  Widget build(BuildContext context) {
    final contact = conversation.contact;
    final lastMessage = conversation.lastMessage;
    final hasUnread = conversation.unreadCount > 0;
    
    return InkWell(
      onTap: onTap,
      child: Container(
        padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
        decoration: BoxDecoration(
          color: isSelected 
              ? ThemeConfig.primaryLightColor.withOpacity(0.1)
              : Colors.transparent,
          border: const Border(
            bottom: BorderSide(
              color: ThemeConfig.borderColor,
              width: 0.5,
            ),
          ),
        ),
        child: Row(
          children: [
            // 头像
            Stack(
              children: [
                // 使用CircleAvatar替代NetworkImage
                CircleAvatar(
                  radius: 24,
                  backgroundColor: _getAvatarColor(contact.username),
                  child: Text(
                    _getInitials(contact.username),
                    style: const TextStyle(
                      color: Colors.white,
                      fontWeight: FontWeight.bold,
                      fontSize: 16,
                    ),
                  ),
                ),
                if (contact.isOnline)
                  Positioned(
                    right: 0,
                    bottom: 0,
                    child: Container(
                      width: 12,
                      height: 12,
                      decoration: BoxDecoration(
                        color: Colors.green,
                        shape: BoxShape.circle,
                        border: Border.all(
                          color: Colors.white,
                          width: 2,
                        ),
                      ),
                    ),
                  ),
              ],
            ),
            
            const SizedBox(width: 12),
            
            // 对话内容
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  // 联系人名称和时间
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Text(
                        contact.username,
                        style: TextStyle(
                          fontWeight: hasUnread ? FontWeight.bold : FontWeight.normal,
                          fontSize: 16,
                        ),
                      ),
                      Text(
                        _formatTime(conversation.updatedAt),
                        style: TextStyle(
                          fontSize: 12,
                          color: hasUnread 
                              ? ThemeConfig.primaryColor
                              : ThemeConfig.textSecondaryColor,
                        ),
                      ),
                    ],
                  ),
                  
                  const SizedBox(height: 4),
                  
                  // 最后一条消息和未读数
                  Row(
                    children: [
                      Expanded(
                        child: lastMessage != null
                            ? Text(
                                lastMessage.content,
                                maxLines: 1,
                                overflow: TextOverflow.ellipsis,
                                style: TextStyle(
                                  fontSize: 14,
                                  color: hasUnread 
                                      ? ThemeConfig.textPrimaryColor
                                      : ThemeConfig.textSecondaryColor,
                                ),
                              )
                            : const Text(
                                '暂无消息',
                                style: TextStyle(
                                  fontSize: 14,
                                  color: ThemeConfig.textDisabledColor,
                                  fontStyle: FontStyle.italic,
                                ),
                              ),
                      ),
                      if (hasUnread)
                        Container(
                          padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 2),
                          decoration: BoxDecoration(
                            color: ThemeConfig.primaryColor,
                            borderRadius: BorderRadius.circular(10),
                          ),
                          child: Text(
                            conversation.unreadCount.toString(),
                            style: const TextStyle(
                              color: Colors.white,
                              fontSize: 12,
                              fontWeight: FontWeight.bold,
                            ),
                          ),
                        ),
                    ],
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
  
  // 格式化时间
  String _formatTime(DateTime time) {
    final now = DateTime.now();
    final difference = now.difference(time);
    
    if (difference.inDays > 7) {
      // 超过一周，显示日期
      return '${time.month}/${time.day}';
    } else if (difference.inDays > 0) {
      // 超过一天，显示星期
      final weekdays = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
      return weekdays[time.weekday % 7];
    } else {
      // 当天，显示时间
      return '${time.hour.toString().padLeft(2, '0')}:${time.minute.toString().padLeft(2, '0')}';
    }
  }
  
  // 获取用户名首字母
  String _getInitials(String username) {
    if (username.isEmpty) return '';
    return username[0].toUpperCase();
  }
  
  // 根据用户名生成颜色
  Color _getAvatarColor(String username) {
    final colors = [
      Colors.blue,
      Colors.green,
      Colors.orange,
      Colors.purple,
      Colors.teal,
      Colors.pink,
      Colors.indigo,
      Colors.cyan,
      Colors.amber,
      Colors.deepPurple,
    ];
    
    // 使用用户名的哈希值来选择颜色
    final index = username.hashCode % colors.length;
    return colors[index.abs()];
  }
} 